package tests;


import javax.management.AttributeNotFoundException;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import exceptions.InvalidAddressCity;
import exceptions.InvalidAddressComplement;
import exceptions.InvalidAddressCountry;
import exceptions.InvalidAddressNeighborhood;
import exceptions.InvalidAddressState;
import exceptions.InvalidAddressStreet;
import exceptions.InvalidAtributException;
import exceptions.InvalidKeyException;
import exceptions.InvalidLogin;
import exceptions.InvalidSession;
import exceptions.InvalidUserAddress;
import exceptions.InvalidUserException;
import exceptions.InvalidUserName;
import exceptions.InvalidUserUsername;
import exceptions.SessionDoesNotExist;
import exceptions.UserAlreadyExistsException;
import exceptions.UserNotExistsException;

import root.SessionManager;
import root.user.User;
import root.user.UserManager;
import strategy.AbstractStrategyFactory;

public class UserManagerTest {
	UserManager userManager;
	SessionManager session;
	final String country = "Brasil";
	final String state = "Paraiba";
	final String city = "Joao Pessoa";
	final String neighborhood = "Manaira";
	final String street = "Rua Joaquim";
	final int number = 333;
	final String complement = "";
	String address = "oi";
	
	@Before
	public void setUpBefore() throws InvalidAddressCountry, InvalidAddressState, InvalidAddressCity, InvalidAddressNeighborhood, InvalidAddressStreet, InvalidAddressComplement {
		userManager = UserManager.getUserManager();
		session = SessionManager.getSessionManager();
	}
	
	@Test (expected=UserAlreadyExistsException.class)
	public void testAddUser() throws InvalidUserUsername, InvalidUserAddress, UserAlreadyExistsException, InvalidUserName, InvalidUserException{
		userManager.createUser("guthyerrz", "guthyerrz1", address);
		userManager.createUser("guthyerrz", "maciel", address);
	}
	@Test
	public void testAddUser3() throws InvalidUserUsername, InvalidUserAddress, UserAlreadyExistsException, InvalidUserName, InvalidUserException{

		userManager.createUser("guthyerrz3", "guthyerrz3", address);		
		Assert.assertEquals(true,userManager.hasUser("guthyerrz3") );
		Assert.assertEquals(false,userManager.hasUser("guthyerrz2"));
		userManager.createUser("guthyerrz2", "guthyerrz2", address);
		Assert.assertEquals(true,userManager.hasUser("guthyerrz2"));
		
	}
	@Test(expected=UserNotExistsException.class)
	public void testRemoveUser() throws InvalidUserUsername, InvalidUserName, InvalidUserAddress, UserNotExistsException{
		userManager.removeUser("guthyerrz6");
	}
	@Test
	public void testRemoveUser2() throws InvalidUserUsername, InvalidUserName, InvalidUserAddress, UserNotExistsException, UserAlreadyExistsException, InvalidUserException{
		userManager.createUser("guthyerrz6", "guthyerrz6", address);
		Assert.assertEquals(true, userManager.hasUser("guthyerrz6"));
		userManager.removeUser("guthyerrz6");
		Assert.assertEquals(false, userManager.hasUser("guthyerrz6"));
	}
	
	@Test
	public void testGetUser() throws InvalidUserUsername, InvalidUserName, InvalidUserAddress, UserAlreadyExistsException, InvalidUserException, UserNotExistsException{
		User usr = userManager.createUser("guthyerrz8", "guthyerrz8", address);
		User usr2 = new User("guthyerrz8", "guthyerrz", address,AbstractStrategyFactory.getInstance());
		Assert.assertEquals(usr2,userManager.getUser("guthyerrz8"));
		Assert.assertEquals(usr, usr2);
		
	}
	
	@Test
	public void testGetAtributo() throws AttributeNotFoundException, InvalidUserUsername, UserNotExistsException, InvalidAtributException{
		Assert.assertEquals("guthyerrz1", userManager.getAttribute("guthyerrz", "nome"));
		Assert.assertEquals("oi", userManager.getAttribute("guthyerrz", "endereco"));
		
	}
	
	@Test
	public void testLocalizarUser() throws InvalidLogin, InvalidSession, InvalidAtributException, SessionDoesNotExist, UserNotExistsException, InvalidKeyException, InvalidUserUsername, InvalidUserName, InvalidUserAddress, UserAlreadyExistsException, InvalidUserException{
		String id=session.createSession("guthyerrz");
			Assert.assertEquals("guthyerrz3 - oi; guthyerrz2 - oi; guthyerrz8 - oi",userManager.findUser(id, "guthy", "nome"));
		Assert.assertEquals("guthyerrz3 - oi; guthyerrz2 - oi; guthyerrz8 - oi",userManager.findUser(id, "oi", "endereco"));
	}

}
